#!/usr/bin/python
"""
Program to add uncat template to images without categories at commons.

See imagerecat.py to add these images to categories.

This script is working on the given site, so if the commons should be handled,
the site commons should be given and not a Wikipedia or similar.

&params;
"""
#
# (C) Pywikibot team, 2008-2021
#
# Distributed under the terms of the MIT license.
#
from contextlib import suppress

import pywikibot
from pywikibot import pagegenerators
from pywikibot.exceptions import EditConflictError, LockedPageError


docuReplacements = {
    '&params;': pagegenerators.parameterHelp,
}

# Probably unneeded because these are hidden categories. Have to figure it out.
ignoreCategories = ['[[Category:CC-BY-SA-3.0]]',
                    '[[Category:GFDL]]',
                    '[[Category:Media for cleanup]]',
                    '[[Category:Media lacking a description]]',
                    '[[Category:Media lacking author information]]',
                    '[[Category:Media lacking a description]]',
                    '[[Category:Self-published work]]']

# Don't bother to put the template on an image with one of these templates
skipTemplates = ['Delete',
                 'Nocat',
                 'No license',
                 'No permission since',
                 'No source',
                 'No source since',
                 'Uncategorized',
                 'Uncat']

# Ignore templates in this long list when looking for relevant categories
ignoreTemplates = ['1000Bit',
                   '1922 cyc',
                   '2MASS',
                   'Aa',
                   'Ab',
                   'AbuGhraibPic',
                   'ADRM',
                   'ADRM2',
                   'AerialPhotograph-mlitJP',
                   'Af',
                   'Agência Brasil',
                   'AgenciaBrasil',
                   'AgenciaCamaraBr',
                   'AgenciaSenadoBr',
                   'Ak',
                   'AL2TB',
                   'Alabordache',
                   'Alexj2002C220PD',
                   'Alexj2002InkscapePD',
                   'Aln',
                   'Als',
                   'Am',
                   'An',
                   'Ang',
                   'Anonymous work',
                   'AnotherAuthor',
                   'Apache',
                   'Ar',
                   'Arc',
                   'Arn',
                   'Artistic-2',
                   'As',
                   'Ashipilin',
                   'AskedForInfo',
                   'Ast',
                   'AstronautPhoto',
                   'Attribution',
                   'Attribution Entomart',
                   'Attribution-Ubisoft',
                   'Auroranorthlicense',
                   'Author-de',
                   'Autorisation photos aériennes Francis Leroy',
                   'Autotravel',
                   'Av',
                   'Avk',
                   'Ay',
                   'Az',
                   'Ba',
                   'BadJPEG',
                   'Bar',
                   'BArch-License',
                   'BArch-link',
                   'Bat-smg',
                   'Bcl',
                   'Be',
                   'Be-tarask',
                   'Be-x-old',
                   'BeeldbankVenW',
                   'Bg',
                   'Bh',
                   'Bi',
                   'Blue Marble',
                   'Bm',
                   'Bmz',
                   'Bn',
                   'Bo',
                   'Botev license',
                   'Botev relicensed',
                   'BotMoveToCommons',
                   'Bpy',
                   'Br',
                   'Bs',
                   'BSD',
                   'BSDwithdisclaimer',
                   'Byp',
                   'C0',
                   'Ca',
                   'CatedralDeSal',
                   'Cc',
                   'CC-AR-Presidency',
                   'Cc-by',
                   'Cc-by-1.0',
                   'Cc-by-1.0-nl',
                   'Cc-by-2.0',
                   'Cc-by-2.0-at',
                   'Cc-by-2.0-be',
                   'Cc-by-2.0-br',
                   'Cc-by-2.0-cl',
                   'Cc-by-2.0-de',
                   'Cc-by-2.0-es',
                   'Cc-by-2.0-fr',
                   'Cc-by-2.0-it',
                   'Cc-by-2.0-kr',
                   'Cc-by-2.0-nl',
                   'Cc-by-2.0-uk',
                   'Cc-by-2.1-au',
                   'Cc-by-2.1-es',
                   'Cc-by-2.1-jp',
                   'Cc-by-2.5',
                   'Cc-by-2.5-bg',
                   'Cc-by-2.5-br',
                   'Cc-by-2.5-dk',
                   'Cc-by-2.5-es',
                   'Cc-by-2.5-in',
                   'Cc-by-2.5-it',
                   'Cc-by-2.5-my',
                   'Cc-by-2.5-nl',
                   'Cc-by-2.5-pl',
                   'Cc-by-2.5-se',
                   'Cc-by-2.5,2.0,1.0',
                   'Cc-by-3.0',
                   'Cc-by-3.0-gr',
                   'Cc-by-3.0-IndiaFM',
                   'Cc-by-3.0-nl',
                   'Cc-by-3.0-rs',
                   'Cc-by-3.0-us',
                   'Cc-by-3.0,2.5,2.0,1.0',
                   'Cc-by-all',
                   'Cc-by-nc-sa-2.0-dual',
                   'Cc-by-sa',
                   'Cc-by-sa-1.0',
                   'Cc-by-sa-1.0-fi',
                   'Cc-by-sa-1.0-tw',
                   'Cc-by-sa-2.0',
                   'Cc-by-sa-2.0-at',
                   'Cc-by-sa-2.0-au',
                   'Cc-by-sa-2.0-be',
                   'Cc-by-sa-2.0-br',
                   'Cc-by-sa-2.0-ca',
                   'Cc-by-sa-2.0-cl',
                   'Cc-by-sa-2.0-de',
                   'Cc-by-sa-2.0-es',
                   'Cc-by-sa-2.0-fr',
                   'Cc-by-sa-2.0-it',
                   'Cc-by-sa-2.0-kr',
                   'Cc-by-sa-2.0-nl',
                   'Cc-by-sa-2.0-tw',
                   'Cc-by-sa-2.0-uk',
                   'Cc-by-sa-2.1-au',
                   'Cc-by-sa-2.1-es',
                   'Cc-by-sa-2.1-jp',
                   'Cc-by-sa-2.5',
                   'CC-BY-SA-2.5',
                   'Cc-by-sa-2.5-ar',
                   'Cc-by-sa-2.5-au',
                   'Cc-by-sa-2.5-bg',
                   'Cc-by-sa-2.5-br',
                   'Cc-by-sa-2.5-ca',
                   'Cc-by-sa-2.5-ch',
                   'Cc-by-sa-2.5-cl',
                   'Cc-by-sa-2.5-cn',
                   'Cc-by-sa-2.5-de',
                   'Cc-by-sa-2.5-dk',
                   'Cc-by-sa-2.5-es',
                   'Cc-by-sa-2.5-hu',
                   'Cc-by-sa-2.5-in',
                   'Cc-by-sa-2.5-it',
                   'Cc-by-sa-2.5-mx',
                   'Cc-by-sa-2.5-my',
                   'Cc-by-sa-2.5-nl',
                   'Cc-by-sa-2.5-pl',
                   'Cc-by-sa-2.5-pt',
                   'Cc-by-sa-2.5-se',
                   'Cc-by-sa-2.5-si',
                   'Cc-by-sa-2.5-tw',
                   'Cc-by-sa-2.5,1.0',
                   'Cc-by-sa-2.5,2.0,1.0',
                   'Cc-by-sa-2.5,2.0,1.0-no-link',
                   'Cc-by-sa-3.0',
                   'Cc-by-sa-3.0-gr',
                   'Cc-by-sa-3.0-nl',
                   'Cc-by-sa-3.0-rs',
                   'Cc-by-sa-3.0-tw',
                   'Cc-by-sa-3.0-us',
                   'Cc-by-sa-3.0,2.5,2.0,1.0',
                   'Cc-by-sa-3.0,2.5,2.0,1.0-no-link',
                   'Cc-by-sa-all',
                   'Cc-by-sa-jul',
                   'CC-LukeFord',
                   'Cc-pd',
                   'Cc-sa-1.0',
                   'Cc-zero',
                   'Cdo',
                   'Ce',
                   'Ceb',
                   'CeCILL',
                   'CERN-CMS',
                   'Ch',
                   'China-PD',
                   'Cho',
                   'Chr',
                   'Chuck',
                   'Chy',
                   'Cityview',
                   'Cityview2',
                   'Civertan license',
                   'Claude',
                   'CNG',
                   'Co',
                   'Coa-Germany-b1945',
                   'Coloured SVG boxes',
                   'Common Public License',
                   'Contact-VP',
                   'Convert to SVG',
                   'CopyAttribEmail',
                   'Copyright by Wikimedia',
                   'Copyright jalpeyrie',
                   'Copyrighted free use',
                   'Copyrighted free use provided that',
                   'Copyrighted IOC',
                   'CopyrightedFreeUse',
                   'CopyrightedFreeUse-Link',
                   'Cr',
                   'Created by ForrestSjap',
                   'Created with Inkscape',
                   'Creator',
                   'Credit line',
                   'Credits-panoramafotos.net',
                   'Crh',
                   'Crh-cyrl',
                   'Croatian currency',
                   'Cs',
                   'Csb',
                   'Cu',
                   'Cv',
                   'Cy',
                   'Cz',
                   'Da',
                   'Date',
                   'De',
                   'De-cc-by-sa-2.0',
                   'Debora Cordeiro',
                   'Diq',
                   'DMonniaux',
                   'DMonniaux-DADVSI-20060507',
                   'Dsb',
                   'DSL',
                   'Dum',
                   'Dv',
                   'Dz',
                   'EdwardCurtis',
                   'Ee',
                   'EEK banknote',
                   'EEK coin',
                   'El',
                   'Elephants Dream',
                   'Eml',
                   'En',
                   'En icon',
                   'Eo',
                   'EPL',
                   'Erin Silversmith Licence',
                   'Es',
                   'EST-Land Board',
                   'Estremeñu',
                   'Et',
                   'Eu',
                   'EU location',
                   'Euro coin common face',
                   'Europe location',
                   'Expat',
                   'Ext',
                   'Fa',
                   'FAL',
                   'Fancyfootworklicense',
                   'Faroe stamps',
                   'Fcb981c',
                   'Ff',
                   'Fi',
                   'FirefoxWiki',
                   'Fiu-vro',
                   'Fj',
                   'Flag-Germany-b1945',
                   'Flickr',
                   'Flickr-change-of-license',
                   'Flickr-unfree-but',
                   'Flickrreview',
                   'Fo',
                   'FOLP',
                   'FOP',
                   'Fr',
                   'Fr icon',
                   'Frc',
                   'Free screenshot',
                   'Frp',
                   'Fur',
                   'FWL',
                   'Fy',
                   'Ga',
                   'Gag',
                   'Gakk Copyright',
                   'Gan',
                   'Gd',
                   'Geograph',
                   'GFDL',
                   'GFDL or cc-by-nc-sa',
                   'GFDL or cc-by-nc-sa/2.5',
                   'GFDL-1.2',
                   'GFDL-1.2-en',
                   'GFDL-CC-triple',
                   'GFDL-DD',
                   'GFDL-en',
                   'GFDL-GMT',
                   'GFDL-Institut de Qualitat Agroalimentària',
                   'GFDL-IS',
                   'GFDL-it',
                   'GFDL-ja',
                   'GFDL-Landsat-Kashmir3d',
                   'GFDL-OpenGeoDB',
                   'GFDL-retouched',
                   'GFDL-Samoborac',
                   'GFDL-self',
                   'GFDL-Self',
                   'GFDL-self-en',
                   'GFDL-user',
                   'GFDL-user-als',
                   'GFDL-user-ar',
                   'GFDL-user-bat-smg',
                   'GFDL-user-bs',
                   'GFDL-user-cs',
                   'GFDL-user-da',
                   'GFDL-user-de',
                   'GFDL-user-el',
                   'GFDL-user-en',
                   'GFDL-user-en-no-disclaimers',
                   'GFDL-user-en-note',
                   'GFDL-user-en-with-disclaimers',
                   'GFDL-user-es',
                   'GFDL-user-fa',
                   'GFDL-user-fi',
                   'GFDL-user-fr',
                   'GFDL-user-gl',
                   'GFDL-user-he',
                   'GFDL-user-hi',
                   'GFDL-user-hu',
                   'GFDL-user-id',
                   'GFDL-user-it',
                   'GFDL-user-ja',
                   'GFDL-user-ko',
                   'GFDL-user-lt',
                   'GFDL-user-nl',
                   'GFDL-user-nn',
                   'GFDL-user-no',
                   'GFDL-user-pl',
                   'GFDL-user-pt',
                   'GFDL-user-ru',
                   'GFDL-user-sk',
                   'GFDL-user-sq',
                   'GFDL-user-tr',
                   'GFDL-user-uk',
                   'GFDL-user-vi',
                   'GFDL-user-vls',
                   'GFDL-user-w',
                   'GFDL-user-zh',
                   'Giovannino Copyright',
                   'Gl',
                   'Glk',
                   'Gloumouth1Credit',
                   'Gn',
                   'GNOME-icon-theme',
                   'Got',
                   'GPL',
                   'GPLv2 only',
                   'GPLv3',
                   'Gr',
                   'Grays Anatomy plate',
                   'Gu',
                   'Gv',
                   'GWArt',
                   'GWArtOld',
                   'GWPDA',
                   'Ha',
                   'HAER',
                   'Hak',
                   'Halibutt Copyright GFDL',
                   'Halibutt Copyright Ogg',
                   'HALS',
                   'Harry',
                   'Harry2',
                   'Haw',
                   'He',
                   'Heraldic Badge',
                   'Hi',
                   'Hiuppo Copyright GFDL',
                   'Ho',
                   'Hr',
                   'Hsb',
                   'Ht',
                   'Hu',
                   'Hy',
                   'Hz',
                   'Ia',
                   'Ibiblio-Hyperwar.org',
                   'Icelandic currency',
                   'Icelandic stamp',
                   'Id',
                   'Ie',
                   'Ig',
                   'Ii',
                   'Ik',
                   'Ike-latn',
                   'Ilo',
                   'ImageUpload',
                   'Img-confirmation',
                   'INewton',
                   'Information',
                   'Information Picswiss',
                   'Inkscape',
                   'Insignia',
                   'Insignia Catalonia',
                   'Insignia Navarre',
                   'Io',
                   'IPPAR',
                   'Is',
                   'It',
                   'It icon',
                   'Iu',
                   'Ja',
                   'Ja2',
                   'James K. Lindsey permission',
                   'Jbo',
                   'Jean-PolGRANDMONTCredit',
                   'JewishEncyclopedia',
                   'Joergens.mi licence',
                   'JPEG version of PNG',
                   'Jv',
                   'Ka',
                   'Kaa',
                   'Kab',
                   'Kameno doba',
                   'Kg',
                   'Ki',
                   'Kirkeinfo',
                   'Kirkenorge',
                   'Kj',
                   'Kk',
                   'Kl',
                   'Km',
                   'Kn',
                   'Ko',
                   'Kopimi',
                   'Kr',
                   'Krj',
                   'Ks',
                   'Ksh',
                   'Ku',
                   'Kv',
                   'Kw',
                   'Ky',
                   'La',
                   'LA2-Blitz',
                   'Lad',
                   'Lan',
                   'LarsenCopyright',
                   'Lb',
                   'Lbe',
                   'Lfn',
                   'Lg',
                   'LGPL',
                   'Li',
                   'LibriVox public domain',
                   'License',
                   'Lij',
                   'LinuxeristCopyright',
                   'Lld',
                   'Lmo',
                   'Ln',
                   'Lo',
                   'LOC-image',
                   'LOC-pchrom',
                   'LOC-prok',
                   'LOC-prokc',
                   'Location',
                   'Location dec',
                   'Location dms',
                   'LocationRequired',
                   'Lothar1976',
                   'Loz',
                   'Ls',
                   'Lt',
                   'Lueger',
                   'Lv',
                   'LviatourCredit',
                   'Map of Japan-Shigenobu AOKI',
                   'Map-Austria-GNu',
                   'Marine-marchande.net',
                   'MartinX',
                   'Master son',
                   'MdB',
                   'Mdf',
                   'Met.no',
                   'Mg',
                   'Mh',
                   'Mi',
                   'Mildenhall',
                   'MindInfo',
                   'MIT',
                   'Mk',
                   'Ml',
                   'MLDoan',
                   'Mn',
                   'Mo',
                   'MoengCredit',
                   'Money-EU banknote',
                   'Money-Eu',
                   'Money-REAL',
                   'MORS',
                   'MPL',
                   'Mr',
                   'Ms',
                   'Mt',
                   'MTL',
                   'Museum.ru',
                   'My',
                   'Myv',
                   'Mzn',
                   'Na',
                   'Nah',
                   'Namespace',
                   'NAMESPACE',
                   'Nap',
                   'NAUMANN',
                   'Nb',
                   'Nds',
                   'Nds-nl',
                   'Ne',
                   'NetMarine',
                   'New',
                   'Newsflash Photo',
                   'Ng',
                   'NGC7000',
                   'NGruev',
                   'NGW',
                   'Ngw2',
                   'Nickshanks-cc-by-2.0-email',
                   'Njegos.org',
                   'Nl',
                   'Nl icon',
                   'Nn',
                   'Nn icon',
                   'No',
                   'No icon',
                   'Non',
                   'Norges Golfforbund',
                   'Norwegian coat of arms',
                   'Not-PD-US-URAA',
                   'Nov',
                   'Nrbelex Photo',
                   'Nrbelex Photo 400D',
                   'Nrm',
                   'Nso',
                   'Nv',
                   'Ny',
                   'NYC Subway map',
                   'NYPL-image-full',
                   'OAL',
                   'Object location',
                   'Oc',
                   'OldOS',
                   'Olessi Copyright Ogg',
                   'Om',
                   'Open Beelden',
                   'Open Font',
                   'Openphotoreview',
                   'Or',
                   'Originally uploaded',
                   'Os',
                   'OsborneFossils',
                   'OTrondal',
                   'OTRS',
                   'Otrs pending',
                   'OTRS pending',
                   'Own',
                   'Own work',
                   'Pa',
                   'Pag',
                   'PAGENAME',
                   'PAGENAMEE',
                   'Painting',
                   'Pam',
                   'Pap',
                   'Parlament.ch',
                   'PAshieldsource',
                   'PBresseler',
                   'PCL',
                   'PD',
                   'PD Colombia',
                   'PD patents',
                   'PD-1923',
                   'PD-AB-exempt',
                   'PD-Abdul Hamid',
                   'PD-AIGA',
                   'PD-Albania-exempt',
                   'PD-Algeria',
                   'PD-AM-exempt',
                   'PD-ancient-script',
                   'PD-AR-Anonymous',
                   'PD-AR-Movie',
                   'PD-AR-Photo',
                   'PD-Archivesnormandie',
                   'PD-Armenia',
                   'PD-Art',
                   'PD-art',
                   'PD-art-life-70',
                   'PD-art-US',
                   'PD-Art-YorckProject',
                   'PD-Australia',
                   'PD-AustrianGov',
                   'PD-author',
                   'PD-AZ-exempt',
                   'PD-Azerbaijan',
                   'PD-Bahrain',
                   'PD-Bain',
                   'PD-Bangladesh',
                   'PD-because',
                   'PD-BH-exempt',
                   'PD-BnFMandragorePic',
                   'PD-Brady-Handy',
                   'PD-Brazil-media',
                   'PD-BrazilGov',
                   'PD-Britannica',
                   'PD-Brockhaus&Efron',
                   'PD-BulgarianGov',
                   'PD-BW',
                   'PD-BY-exempt',
                   'PD-Canada',
                   'PD-Canada-creator',
                   'PD-Canada-Crown',
                   'PD-Canada-photo',
                   'PD-Canada-stamp',
                   'PD-Challenger Report',
                   'PD-chem',
                   'PD-Chile',
                   'PD-China',
                   'PD-Coa-Finland',
                   'PD-Coa-Germany',
                   'PD-Coa-Hungary',
                   'PD-Coins-Krenzer',
                   'PD-collective anonymous work',
                   'PD-copyright holder',
                   'PD-Croatia',
                   'PD-CroatiaGov',
                   'PD-CSIRO',
                   'PD-Cuba',
                   'PD-CzechGov',
                   'PD-DAUI',
                   'PD-DBZ stamps',
                   'PD-Demis',
                   'PD-Denmark50',
                   'PD-DenmarkEVH',
                   'PD-Detroit',
                   'PD-Deutsche Bundespost stamps',
                   'PD-Edison Records',
                   'PD-EE-exempt',
                   'PD-EEA',
                   'PD-Egypt',
                   'PD-EstoniaPub',
                   'PD-EU-no author disclosure',
                   'PD-Feldman-1905',
                   'PD-Fiji',
                   'PD-Finland',
                   'PD-Finland50',
                   'PD-FinlandGov',
                   'PD-flag',
                   'PD-Flag',
                   'PD-flag-50',
                   'PD-Flag-Germany',
                   'PD-FLGov',
                   'PD-font',
                   'PD-FOP-DE',
                   'PD-Fröléens konversationslexikon',
                   'PD-GallicaPic',
                   'PD-GDR stamps',
                   'PD-GE-exempt',
                   'PD-Generic',
                   'PD-German Empire stamps',
                   'PD-German postmarks',
                   'PD-German stamps',
                   'PD-GermanGov',
                   'PD-Google books',
                   'PD-Gottscho',
                   'PD-GreekGov',
                   'PD-Gutenberg',
                   'PD-heirs',
                   'PD-Highsmith',
                   'PD-HK',
                   'PD-HK-PR',
                   'PD-HU-exempt',
                   'PD-Hubble',
                   'PD-IDGov',
                   'PD-IDOld-Art29',
                   'PD-IDOld-Art30',
                   'PD-imf.org',
                   'PD-India',
                   'PD-ineligible',
                   'PD-Internationale',
                   'PD-Iran',
                   'PD-Iraq',
                   'PD-Ireland',
                   'PD-IrishGov',
                   'PD-Israel',
                   'PD-Israel-Photo',
                   'PD-ItalyGov',
                   'PD-Japan',
                   'PD-Japan-exempt',
                   'PD-Japan-film',
                   'PD-Japan-oldphoto',
                   'PD-Japan-organization',
                   'PD-Jordan',
                   'PD-Jordan-Photo',
                   'PD-JORF',
                   'PD-JORF-nor-conso',
                   'PD-Kenya',
                   'PD-KG-exempt',
                   'PD-Kuwait',
                   'PD-KZ-exempt',
                   'PD-Lebanon',
                   'PD-Libya',
                   'PD-link',
                   'PD-LOC',
                   'PD-Look',
                   'PD-LosAlamos',
                   'PD-Lozinski',
                   'PD-LT-exempt',
                   'PD-LV-exempt',
                   'PD-MacaoGov',
                   'PD-magic',
                   'PD-Malawi',
                   'PD-Manchukuo-stamps',
                   'PD-MapLibrary',
                   'PD-Mauritius',
                   'PD-MD-exempt',
                   'PD-Mexico',
                   'PD-Mexico-NIP',
                   'PD-Meyers',
                   'PD-Meyers-5th-edition',
                   'PD-Meyers-6th-edition',
                   'PD-MNGov',
                   'PD-MO',
                   'PD-money-Romania',
                   'PD-Morocco',
                   'PD-Namibia',
                   'PD-National Photo Company',
                   'PD-Nigeria',
                   'PD-NL-gemeentevlag',
                   'PD-NL-gemeentewapen',
                   'PD-NL-Gov',
                   'PD-NL-verkeersbord',
                   'PD-Nordens Flora',
                   'PD-North Korea',
                   'PD-Norway50',
                   'PD-NorwayGov',
                   'PD-NTBB',
                   'PD-NZ',
                   'PD-NZ-50-years',
                   'PD-old',
                   'Pd-old',
                   'PD-Old',
                   'PD-old-100',
                   'PD-old-50',
                   'PD-old-70',
                   'PD-old-75',
                   'PD-old-80',
                   'PD-old-Edition',
                   'PD-Oman',
                   'PD-OpenClipart',
                   'PD-Orgelbrand',
                   'PD-Ornament1898',
                   'PD-Pakistan',
                   'PD-PCL-portraits',
                   'PD-PDFnet',
                   'PD-PDphoto.org',
                   'PD-Peru',
                   'PD-Philippines',
                   'PD-PhilippinesPubDoc',
                   'PD-PMR-exempt',
                   'PD-Polish',
                   'PD-Polishsymbol',
                   'PD-PRC-exempt',
                   'PD-PT',
                   'PD-retouched-user',
                   'PD-retouched-user-w',
                   'PD-RO-exempt',
                   'PD-ROC-exempt',
                   'PD-ROC-Traffic Indicating Lines',
                   'PD-ROC-Traffic Signs',
                   'PD-RU-exempt',
                   'PD-RusEmpire',
                   'PD-Russia',
                   'PD-Russia-2008',
                   'PD-SAGov',
                   'PD-Saudi Arabia',
                   'PD-SBH',
                   'PD-scan',
                   'PD-Scan',
                   'PD-SCGGov',
                   'PD-ScottForesman',
                   'PD-ScottForesman-raw',
                   'PD-script',
                   'PD-Seal-Germany',
                   'PD-Seattle-Neighborhood-Atlas',
                   'PD-SeaWiFS',
                   'PD-self',
                   'PD-self2',
                   'PD-SerbiaGov',
                   'PD-Seychelles',
                   'PD-SFJ',
                   'PD-shape',
                   'PD-Sjöfartsverket',
                   'PD-SlovakGov',
                   'PD-South Korea',
                   'PD-South-Africa',
                   'PD-Soviet-revised',
                   'PD-SRBGov',
                   'PD-STFP',
                   'PD-StVZVO',
                   'PD-Sudan',
                   'PD-Suomen',
                   'PD-Sweden',
                   'PD-Sweden-1969',
                   'PD-Sweden-photo',
                   'PD-Sweden-self',
                   'PD-Switzerland-official',
                   'PD-Switzerland-photo',
                   'PD-Syria',
                   'PD-Taiwan',
                   'PD-text',
                   'PD-textlogo',
                   'PD-TH-exempt',
                   'PD-Thailand',
                   'PD-TJ-exempt',
                   'PD-TK-exempt',
                   'PD-TR-Gov',
                   'PD-Traditional',
                   'PD-Tunisia',
                   'PD-UA-exempt',
                   'PD-UEA',
                   'PD-Uganda',
                   'PD-Ugglan',
                   'PD-UK-known',
                   'PD-UK-unknown',
                   'PD-UKGov',
                   'PD-Ukraine',
                   'PD-UN',
                   'PD-United Arab Emirates',
                   'PD-URAA',
                   'PD-Uruguay',
                   'PD-US',
                   'PD-US-1978-89',
                   'PD-US-flag',
                   'PD-US-no notice',
                   'PD-US-not renewed',
                   'PD-US-patent',
                   'PD-US-patent-no notice',
                   'PD-US-record',
                   'PD-user',
                   'PD-User',
                   'PD-user-als',
                   'PD-user-ar',
                   'PD-user-ca',
                   'PD-user-cs',
                   'PD-user-da',
                   'PD-user-de',
                   'PD-user-en',
                   'PD-user-fa',
                   'PD-user-fr',
                   'PD-user-he',
                   'PD-user-hi',
                   'PD-user-hr',
                   'PD-user-hu',
                   'PD-user-it',
                   'PD-user-ja',
                   'PD-user-lt',
                   'PD-user-nl',
                   'PD-user-nn',
                   'PD-user-no',
                   'PD-user-pl',
                   'PD-user-pt',
                   'PD-user-ro',
                   'PD-user-ru',
                   'PD-user-sk',
                   'PD-user-sl',
                   'PD-user-th',
                   'PD-user-uk',
                   'PD-user-vls',
                   'PD-user-w',
                   'PD-user-zh',
                   'PD-USGov',
                   'PD-USGov-ARM',
                   'PD-USGov-Atlas',
                   'PD-USGov-Award',
                   'PD-USGov-BLM',
                   'PD-USGov-CIA',
                   'PD-USGov-CIA-WF',
                   'PD-USGov-Congress',
                   'PD-USGov-Congress-AOC',
                   'PD-USGov-Congress-Bio',
                   'PD-USGov-DEA',
                   'PD-USGov-DHS',
                   'PD-USGov-DHS-CG',
                   'PD-USGov-DHS-CGAUX',
                   'PD-USGov-DOC',
                   'PD-USGov-DOC-Census',
                   'PD-USGov-DOE',
                   'PD-USGov-DOJ',
                   'PD-USGov-DOL',
                   'PD-USGov-DOS',
                   'PD-USGov-DOT',
                   'PD-USGov-DVA',
                   'PD-USGov-ED',
                   'PD-USGov-EPA',
                   'PD-USGov-FAA',
                   'PD-USGov-FBI',
                   'PD-USGov-FDA',
                   'PD-USGov-Federal Reserve',
                   'PD-USGov-FEMA',
                   'PD-USGov-FSA',
                   'PD-USGov-FWS',
                   'PD-USGov-HHS',
                   'PD-USGov-HHS-CDC',
                   'PD-USGov-HHS-NIH',
                   'PD-USGov-Interior',
                   'PD-USGov-Interior-HABS',
                   'PD-USGov-Interior-MMS',
                   'PD-USGov-Interior-USBR',
                   'PD-USGov-Interior-USGS-Minerals',
                   'PD-USGov-Military',
                   'PD-USGov-Military award',
                   'PD-USGov-Military-Air Force',
                   'PD-USGov-Military-Air Force Auxiliary',
                   'PD-USGov-Military-Army',
                   'PD-USGov-Military-Army-USACE',
                   'PD-USGov-Military-Army-USACMH',
                   'PD-USGov-Military-Army-USAIOH',
                   'PD-USGov-Military-Badge',
                   'PD-USGov-Military-Coast Guard',
                   'PD-USGov-Military-DVIC',
                   'PD-USGov-Military-Marines',
                   'PD-USGov-Military-MDA',
                   'PD-USGov-Military-National Guard',
                   'PD-USGov-Military-Navy',
                   'PD-USGov-Military-NGA',
                   'PD-USGov-money',
                   'PD-USGov-MUTCD',
                   'PD-USGov-NASA',
                   'PD-USGov-NASA/copyright',
                   'PD-USGov-NCBI-scienceprimer',
                   'PD-USGov-NIH',
                   'PD-USGov-NIST',
                   'PD-USGov-NOAA',
                   'PD-USGov-NPS',
                   'PD-USGov-NRO',
                   'PD-USGov-NSA',
                   'PD-USGov-NSF',
                   'PD-USGov-NTSB',
                   'PD-USGov-POTUS',
                   'PD-USGov-State',
                   'PD-USGov-Treasury',
                   'PD-USGov-TVA',
                   'PD-USGov-USAID',
                   'PD-USGov-USDA',
                   'PD-USGov-USDA-ARS',
                   'PD-USGov-USDA-FS',
                   'PD-USGov-USDA-NAL',
                   'PD-USGov-USDA-NRCS',
                   'PD-USGov-USGS',
                   'PD-USGov-USIA',
                   'PD-USGov-VOA',
                   'PD-USGov-WPA',
                   'PD-USNWR',
                   'PD-UZ-exempt',
                   'PD-Van Vechten',
                   'PD-Vegagerdin',
                   'PD-Venezuela',
                   'PD-Vietnam',
                   'PD-VL-shield',
                   'PD-Vlaams-gemeentewapen',
                   'PD-Vlaamse-gemeentevlag',
                   'PD-VzKat',
                   'PD-WorldWind',
                   'PD-Yemen',
                   'PD-Yugoslavia',
                   'PD-Zimbabwe',
                   'Pd',
                   'Pdc',
                   'Peregrine981',
                   'PermissionOTRS',
                   'PermissionOTRS-ID',
                   'Personality rights',
                   'Photo-by-Wojciechowscy-GFDL',
                   'Photocity.ru',
                   'PhotoManifs-PP',
                   'Photos by Bagn Bygdesamling',
                   'Photos by Kulturhistorisk museum',
                   'Photos by Stortinget',
                   'Photos by the Norwegian Museum of Cultural History',
                   'Pi',
                   'Picswiss',
                   'Piratpartiet',
                   'Pl',
                   'Pl icon',
                   'PLoS',
                   'Pms',
                   'PNG with JPEG version',
                   'Pnt',
                   'Polish coats of arms by Tadeusz Gajl',
                   'PolishPresidentCopyright',
                   'PolishSenateCopyright',
                   'Polishsymbol',
                   'Politas FZ30',
                   'Portpictures.nl',
                   'Pousbeeld',
                   'Pressefotos Die Gruenen',
                   'Ps',
                   'Pt',
                   'Pt icon',
                   'Pt-br',
                   'Qu',
                   'QualityImage',
                   'RadioSenadoBr',
                   'RetouchedPicture',
                   'Rm',
                   'Rmy',
                   'Rn',
                   'Ro',
                   'Roa-rup',
                   'RomanianMCTICopyright',
                   'RTCNCA License',
                   'Rtl-lang',
                   'Ru',
                   'Rw',
                   'Sa',
                   'Sah',
                   'Sc',
                   'Scn',
                   'Sco',
                   'Sd',
                   'Sdc',
                   'Se',
                   'Seattle Neighborhood Atlas disclaimer',
                   'Second Life',
                   'Sei',
                   'SejmCopyright',
                   'Self',
                   'Self2',
                   'Self2-name',
                   'Self3',
                   'Self4',
                   'Self-GFDL-German',
                   'SelfSA-GFDL-German',
                   'SenadoMexico',
                   'Sergeymila',
                   'Sg',
                   'SgiraldoaCredit',
                   'Sh',
                   'Shi',
                   'Si',
                   'SIB',
                   'Simple',
                   'Simple English',
                   'Sk',
                   'Sl',
                   'Sm',
                   'Sma',
                   'Sn',
                   'So',
                   'Solkoll',
                   'Solkoll 2D',
                   'Solkoll 3D',
                   'South Korean currency',
                   'SpaceShuttle',
                   'SPD-Parteivorstand',
                   'Spui',
                   'Spuiother',
                   'Sq',
                   'Sr',
                   'Srn',
                   'Ss',
                   'St',
                   'Stan Shebs photo',
                   'Stationsweb',
                   'Statistics Netherlands map',
                   'Stielers Handatlas 1891',
                   'Stortinget3',
                   'Stq',
                   'Stratosphere',
                   'Su',
                   'SupersededPNG',
                   'SupersededSVG',
                   'Supported by Wikimedia France',
                   'Sv',
                   'Svensk porträttgalleri',
                   'Svg',
                   'SVG',
                   'SVG-Map Africa by Slomox',
                   'Sw',
                   'Swiss Government Portrait',
                   'SygnaturaPixela',
                   'Szl',
                   'Ta',
                   'Tango',
                   'Taxonavigation',
                   'Tcolphoto',
                   'Te',
                   'Texas State Highway',
                   'Tet',
                   'Tg',
                   'Tgl',
                   'Tintazul',
                   'Th',
                   'ThomasBredolCredit2',
                   'ThomasBredolCredit3',
                   'Ti',
                   'Tk',
                   'Tl',
                   'Tlx',
                   'Tn',
                   'To',
                   'TomCorserCredit',
                   'Tpi',
                   'Tr',
                   'Trademark',
                   'Trademarked',
                   'Ts',
                   'Tt',
                   'Tum',
                   'TVSenadoBr',
                   'Tw',
                   'Ty',
                   'Tyv',
                   'Udm',
                   'Ug',
                   'Uk',
                   'UN map',
                   'Ur',
                   'User:FlickreviewR/reviewed-pass',
                   'User:Fir0002/20D',
                   'USDA',
                   'UWiscCIMSS',
                   'Uz',
                   'Ve',
                   'Vec',
                   'Vector-Images.com',
                   'Vector version available',
                   'Vi',
                   'Viollet-le-Duc',
                   'Vl',
                   'Vls',
                   'Vo',
                   'W',
                   'Wa',
                   'Walks.ru',
                   'War',
                   'WEF',
                   'Wikimedia relicensing',
                   'Wikipedia-screenshot',
                   'Wikiportrait',
                   'Wikispecies',
                   'WLA',
                   'Wo',
                   'Wulfstan GFDL',
                   'Wuu',
                   'Www.bordeaux-port.fr',
                   'Www.folketinget.dk',
                   'Www.hotelviewarea.com',
                   'Www.nordenskirker.dk',
                   'Xal',
                   'XGSC image',
                   'Xh',
                   'YAM',
                   'Ydd',
                   'Yi',
                   'Yo',
                   'Yue',
                   'Za',
                   'Zea',
                   'Zh',
                   'Zh-classical',
                   'Zh-cn',
                   'Zh-hans',
                   'Zh-hant',
                   'Zh-hk',
                   'Zh-min-nan',
                   'Zh-sg',
                   'Zh-tw',
                   'Zh-yue',
                   'Zlib',
                   'Zu',
                   'Zxx',
                   ]

puttext = ('\n{{Uncategorized|year={{subst:CURRENTYEAR}}|'
           'month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}}}')
putcomment = 'Please add categories to this image'


def isUncat(page):
    """
    Do we want to skip this page.

    If we found a category which is not in the ignore list it means
    that the page is categorized so skip the page.
    If we found a template which is in the ignore list, skip the page.
    """
    pywikibot.output('Working on ' + page.title())

    for category in page.categories():
        if category not in ignoreCategories:
            pywikibot.output('Got category ' + category.title())
            return False

    for template_with_trail in page.templates():
        # Strip of trailing garbage
        template = template_with_trail.title().rstrip('\n').rstrip()
        if template in skipTemplates:
            # Already tagged with a template, skip it
            pywikibot.output('Already tagged, skip it')
            return False
        if template in ignoreTemplates:
            # template not relevant for categorization
            pywikibot.output('Ignore ' + template)
        else:
            pywikibot.output('Not ignoring ' + template)
            return False
    return True


def addUncat(page):
    """
    Add the uncat template to the page.

    :param page: Page to be modified
    :type page: pywikibot.Page
    """
    newtext = page.get() + puttext
    pywikibot.showDiff(page.get(), newtext)
    with suppress(EditConflictError, LockedPageError):
        page.put(newtext, putcomment)


def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    :param args: command line arguments
    :type args: str
    """
    generator = None

    local_args = pywikibot.handle_args(args)

    site = pywikibot.Site()

    if site.code != 'commons' or site.family.name != 'commons':
        pywikibot.warning('This script is primarily written for Wikimedia '
                          'Commons, but has been invoked with site {}. It '
                          'might work for other sites but there is no '
                          'guarantee that it does the right thing.'
                          .format(site))
        choice = pywikibot.input_choice(
            'How do you want to continue?',
            (('Continue using {}'.format(site), 'c'),
             ('Switch to Wikimedia Commons', 's'),
             ('Quit', 'q')),
            automatic_quit=False)
        if choice == 's':
            site = pywikibot.Site('commons', 'commons')
        elif choice == 'q':
            return False

    gen_factory = pagegenerators.GeneratorFactory(site)

    for arg in local_args:
        param_arg, sep, param_value = arg.partition(':')
        if param_value == '':
            param_value = None
        gen_factory.handle_arg(arg)

    generator = gen_factory.getCombinedGenerator(gen=generator, preload=True)
    if not generator:
        pywikibot.bot.suggest_help(missing_generator=True)
    else:
        site.login()
        for page in generator:
            pywikibot.output(page.title())
            if page.exists() and (page.namespace() == 6) \
                    and (not page.isRedirectPage()):
                if isUncat(page):
                    addUncat(page)


if __name__ == '__main__':
    main()
